var ctx;

function setCtx(ctx) {
	this.ctx = ctx;
}

//验证标题
function checkTitle(){
	var title = $("#title").val();
	//解决ie9以下placeholder与value重复的问题
	if(title == $("title").attr("placeholder")){
		title = "";
	}
	if(title == null || title == ""){
		layer.tips('标题不能为空!', '#title', {
		    tips: 3,
		    time:1000
		});
		return false;
	}else{
		return true;
	}
}

//验证开始时间
function checkBeginTime(){
	var beginTime = $("#beginTime").val();
	//解决ie9以下placeholder与value重复的问题
	if(beginTime == $("beginTime").attr("placeholder")){
		beginTime = "";
	}
	if(beginTime == null || beginTime == ""){
		layer.tips('请选择开始时间!', '#beginTime', {
		    tips: 3,
		    time:1000
		});
		return false;
	}else{
		return true;
	}
}

//验证结束时间
function checkEndTime(){
	var endTime = $("#endTime").val();
	//解决ie9以下placeholder与value重复的问题
	if(endTime == $("endTime").attr("placeholder")){
		endTime = "";
	}
	if(endTime == null || endTime == ""){
		layer.tips('请选择结束时间!', '#endTime', {
		    tips: 3,
		    time:1000
		});
		return false;
	}else{
		return true;
	}
}



//保存或新增方法
var objId; 
function saveAdInfo() {
	var adInfoId = $("#adInfoId").val();
	var title = $("#title").val();
	var adCategoryid = $("#adCategoryid").val();
	var beginTime = $("#beginTime").val();
	var endTime = $("#endTime").val();
	var objType = $("#objType").val();
	if($("#objTypeee").val() == 2){
		objId = $("#goodsId").val();
	}else {
		objId = $("#shopId").val();
	}
	var webUrl = $("#webUrl").val();
	var remark = $("#remark").val();
	var imageLogo = $('#imageLogo').val();

	if(checkTitle()&&checkBeginTime()&&checkEndTime()){
				$.ajax({
					type : "POST",
					url : ctx + "/ad/saveOrUpdateAdInfo",
					cache : false,
					dataType : 'json',
					data: {
						title:title,
						id:adInfoId,
						adCategoryId:adCategoryid,
						beginTime:beginTime,
						endTime:endTime,
						objType:objType,
						objId:objId,
						webUrl:webUrl,
						remark:remark,
						img : imageLogo
					},
					success : function(result) {
						if (result.success) {
							layer.msg("操作成功",{
								icon: 1,
								time: 2000 //2秒关闭（如果不配置，默认是3秒）
							},function(){
								loadContent("/ad/adInfoList");
							});
						} else {
							layer.msg("操作失败",{
								icon: 2
							});
						}
					}
				});
    }
}

//返回上一页
function cancel(){
	loadContent("/ad/adInfoList"); 
}